package com.zengjianjun.oss.platform;

import com.zengjianjun.oss.enums.OssPlatformEnum;
import com.zengjianjun.oss.exception.OssException;
import com.zengjianjun.oss.platform.aliyun.AliYunOssClient;
import com.zengjianjun.oss.platform.param.FileListParam;
import com.zengjianjun.oss.platform.result.FileListResult;
import com.zengjianjun.oss.properties.OssProperties;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

/**
 * @author zengjianjun
 */
@Slf4j
public class OssClient implements IOssClient {

    @Resource
    private AliYunOssClient aliYunOssClient;
    @Resource
    private OssProperties ossProperties;
    private IOssClient client;


    @PostConstruct
    public void init() {
        log.info("oss client init start...");
        OssPlatformEnum type = ossProperties.getType();
        if (type == null) {
            throw OssException.build("oss client init error: type is null");
        }
        if (type == OssPlatformEnum.aliyun) {
            client = aliYunOssClient;
        } else {
            throw OssException.build("oss client init error: unsupported oss client type");
        }
        log.info("oss client load {}", type.name());
        log.info("oss client init success...");
    }

    @Override
    public List<FileListResult> getFileList(FileListParam param) {
        return client.getFileList(param);
    }

    @Override
    public FileListResult getFile(String bucketName, String fileKey) {
        return client.getFile(bucketName, fileKey);
    }

    @Override
    public List<FileListResult> getFullFileList(FileListParam param) {
        return client.getFullFileList(param);
    }

    @Override
    public void download(String bucketName, String fileKey, String filePath) {
        client.download(bucketName, fileKey, filePath);
    }
}
